<?php

/**
 * [PHPFOX_HEADER]
 */
defined('PHPFOX') or exit('NO DICE!');

class Hai_Component_Controller_Index extends Phpfox_Component {

    var $aCartContents;

    public function __construct($aParams) {
        $this->aCartContents = $_SESSION['cart_contents'];
    }

    public function process() {
//        $this->addStore(array(
//            'id' => 5,
//            'title' => 'Store hulu'
//        ));
//        $this->addStore(array(
//            'id' => 5,
//            'title' => 'Store minhhuu'
//        ));
//        $this->addStore(array(
//            'id' => 7,
//            'title' => 'Store shoe'
//        ));
        
        $this->addProduct(array(
            'id' => 10,
            'quantity' => 2,
            'price' => 2.34,
            'title' => 'shoe',
        ), 5);
        
        
        d($_SESSION);
        

        //echo session_id();$_SESSION['hai'] = 'tac';
        //session_id('oq06m3peugnb9kp9e2pdn7rls2');
        die();
        /* Phpfox::getLib('image')->createThumbnail('file/pic/project/golden.jpg',
          'file/pic/project/golden_575.jpg',
          575,
          100,
          false); */
        //Phpfox::getLib('image')->addText('file/pic/project/golden_575.jpg');

        /*
          $connection = Phpfox::getLib('twitterconnect', array('Fz5yaJkPeO54t8gziUKg', '4IZeKEXd4ROQA69iHZmMhMiss1dpBu97ZcxN6eQ36NQ'));
          $request_token = $connection->getRequestToken('http://localhost/phpfox_ushout/index.php?do=/hai/');
          print_r($request_token);die();
         */
        //$request_token = $connection->getRequestToken(OAUTH_CALLBACK);

        /* Phpfox::getLib('image')->addWatermark('file/pic/project/golden_575.jpg');
          Phpfox::getLib('image')->cropImage('file/pic/project/golden.jpg', 'file/pic/project/golden_hi.jpg', 100,100, 0,0, 100);
         */

        /*
          if(isset($_FILES['file'])) {
          print_r($_FILES);

          echo is_uploaded_file($_FILES['file']['tmp_name']);
          echo dirname($_FILES['file']['tmp_name']);
          //copy($_FILES['file']['tmp_name'], $_FILES['file']['name']);
          //move_uploaded_file($_FILES['file']['tmp_name'], $_FILES['file']['name']);

          }
          if(isset($_POST['submit'])) {
          //d($_POST);
          //d(Phpfox::getLib('file')->load('file', array('jpg', 'gif', 'png')));
          //$sFileName = Phpfox::getLib('file')->upload('image', Phpfox::getParam('ad.dir_image'), 10);
          }
         */
    }

    public function isStore($iStoreId) {
        if (array_key_exists($iStoreId, $this->aCartContents)) {
            return TRUE;
        }

        return FALSE;
    }

    public function isProduct($iProductId, $fPrice, $iStoreId) {
        
        if (!isset($this->aCartContents[$iStoreId])) {
            return FALSE;
        }
        
        foreach($this->aCartContents[$iStoreId] as $key=>$item) {
            if($item['id'] == $iProductId && $item['price'] == $fPrice) {
                return TRUE;
            }
        }
        
        return FALSE;
//        if (isset($this->aCartContents[$iStoreId]) && isset($this->aCartContents[$iStoreId][$iProductId]) && ) {
//            return TRUE;
//        }
//
//        return FALSE;
    }

    public function addStore($aStore) {

        $this->aCartContents[$aStore['id']] = array(
            'title' => $aStore['title']
        );
        
        $this->saveCart();
        return TRUE;
    }

    public function addProduct($aProducts, $iStoreId) {
        if (array_key_exists($iStoreId, $this->aCartContents)) {
            $this->aCartContents[$iStoreId][$aProducts['id']] = array(
                'p_id' => uniqid(),
                'id' => $aProducts['store_id'],
                'quantity' => $aProducts['quantity'],
                'title' => $aProducts['title'],
                'price' => $aProducts['price'],
            );
        }
        
        $this->saveCart();
        return TRUE;
    }
    
    public function updateCart($aProducts) {
        foreach($aProducts as $key=>$item) {
            $aKey = explode('|', $key);
            
            if(isset($aKey[0]) && isset($key1) && isset($this->aCartContents[$aKey[0]][$key[1]])) {
                 $this->aCartContents[$aKey[0]][$key[1]]['quantity'] = $item;
            }
        }
        
        return FALSE;
    }
    
    public function _update($aProduct) {
        
    }

    
    
    public function saveCart() {
        unset($this->aCartContents['item']);
        unset($this->aCartContents['total']);

        $iTotal = 0;
        $iItem = 0;
        foreach ($this->aCartContents as $key => $item) {
            if (!is_array($item)) {
                continue;
            }

            unset($item['item']);
            unset($item['total']);
            $sTitlte = $item['title'];
            unset($item['title']);

            $iTotalInStore = 0;
            $iItemInStore = 0;

            foreach ($item as $key2 => $item2) {
                if (!is_array($item2) || !isset($item2['quantity']) || !isset($item2['price'])) {
                    continue;
                }

                $iTotalInStore += ($item2['quantity'] * $item2['price']);
                $iItemInStore += $item2['quantity'];
            }

            $this->aCartContents[$key]['total'] = $iTotalInStore;
            $this->aCartContents[$key]['item'] = $iItemInStore;
            $this->aCartContents[$key]['title'] = $sTitlte;

            $iTotal += $iTotalInStore;
            $iItem += $iItemInStore;
        }

        $this->aCartContents['total'] = $iTotal;
        $this->aCartContents['item'] = $iItem;

        unset($_SESSION['cart_contents']);

        $_SESSION['cart_contents'] = $this->aCartContents;

        return TRUE;
    }
    
    

}